package com.cater.sys.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cater.annotation.SystemControllerLog;
import com.cater.common.PageResult;
import com.cater.query.CommonQuery;
import com.cater.sys.entity.Dictionarydata;
import com.cater.sys.entity.JsonData;
import com.cater.sys.entity.Template;
import com.cater.sys.service.impl.DictionarydataServiceImpl;
import com.cater.sys.service.impl.JsonDataServiceImpl;
import com.cater.sys.service.impl.TemplateServiceImpl;
import com.cater.utils.JsonUtil;
import com.cater.utils.StringUitl;

/**
 * @author 作者: sd
 * @节点模板
 * @version 1.0
 */
@Controller
@RequestMapping("/template")
public class TemplateController {
	@Autowired
	private TemplateServiceImpl service;

	@Autowired
	private JsonDataServiceImpl tableservice;

	@Autowired
	DictionarydataServiceImpl dictionService;

	@RequestMapping("/list")
	private String list() {
		return "sys/template/list";
	}

	@RequestMapping("/listData")
	@ResponseBody
	@SystemControllerLog(description = "查看模板")
	public PageResult<Template> getAll(CommonQuery query, HttpServletRequest request) {
		PageResult<Template> result = service.findPageResult(query);
		return result;
	}

	/**
	 * 添加视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "addView")
	@RequiresPermissions("template:add")
	public String addView(Model model,String type,CommonQuery query) {
		model.addAttribute("type", type);
		return "sys/template/add";
	}

	/**
	 * 添加操作
	 * 
	 * @param Template
	 * @return
	 */
	@RequestMapping(value = "add")
	@RequiresPermissions("template:add")
	@ResponseBody
	@SystemControllerLog(description = "添加模板")
	@Transactional
	public JSONObject add(Template Template, HttpServletRequest request) {
		Map<String, String[]> params = request.getParameterMap();
		JSONObject json = new JSONObject();
		JsonData tb = new JsonData();
		tb.setJsondata(JsonUtil.templateJsonToString(params));
		String tbid = tableservice.saves(tb);
		tb.setId(tbid);
		Template.setJsondatas(tb);
		int id = service.save(Template);
		if (id != 0) {
			json.put("code", 200);
			json.put("message", "success");
		} else {
			json.put("code", 999);
			json.put("message", "fail");
		}
		return json;
	}

	/**
	 * 修改视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "editView")
	@RequiresPermissions("template:update")
	public String editView(String id, Model model, CommonQuery query) {
		Template Template = service.getById(id);
		List<Dictionarydata> dictionarydata = dictionService.getDataByType("sys_modular");
		model.addAttribute("dictionarydata", dictionarydata);
		model.addAttribute("template", Template);
		return "sys/template/update";
	}

	/**
	 * 修改操作
	 * 
	 * @param Template
	 * @return
	 */
	@RequestMapping(value = "update")
	@RequiresPermissions("template:update")
	@ResponseBody
	@SystemControllerLog(description = "修改模板")
	@Transactional
	public JSONObject update(Template Template, HttpServletRequest request) {
		JSONObject json = new JSONObject();
		try {
			Map<String, String[]> params = request.getParameterMap();
			if (Template.getJsondatas() != null && Template.getJsondatas().getId() != null) {
				JsonData tb = tableservice.getById(Template.getJsondatas().getId());
				tb.setJsondata(JsonUtil.templateJsonToString(params));
				tableservice.update(tb);
				Template.setJsondatas(tb);
			}
			service.update(Template);
			json.put("code", 200);
			json.put("message", "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "修改失败");
		}
		return json;
	}

	/**
	 * 删除操作
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	@SystemControllerLog(description = "删除模板")
	public JSONObject delete(String id) {
		JSONObject json = new JSONObject();
		try {
			service.delete(id);
			json.put("code", 200);
			json.put("message", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "删除失败");
		}
		return json;
	}

	@RequestMapping("/tofirst")
	@ResponseBody
	public String tofirst(String value, CommonQuery query) {
		return StringUitl.ToPinyin(value).toUpperCase();
	}

	@RequestMapping("/getAll")
	@ResponseBody
	public List<Template> getAllCommon(CommonQuery query) {
		List<Template> goodstemp = service.getAllCommon(query);
		return goodstemp;
	}

	@RequestMapping("/getByid")
	@ResponseBody
	public Template getByid(String id, CommonQuery query) {
		Template goodstemp = service.getById(id);
		return goodstemp;
	}

	/**
	 * @根据模版类型id获取
	 * @param id
	 * @param query
	 * @return
	 */
	@RequestMapping("/getBytype")
	@ResponseBody
	public List<Template> getBytype(String id, CommonQuery query) {
		List<Template> goodstemp = service.getBytype(id,query);
		return goodstemp;
	}
}
